package com.cwj;

import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author chenwujie
 * @date 2020/10/218:41
 */
public class StringTest {
    public static void main(String[] args) {
//        String s = "比如\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66这个表情长";
        String s = "\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66你好";
        System.out.println(s);
        System.out.println(s.length());
        System.out.println(humanStringLength(s));

        byte b = (byte) 0b1110_0001;
        char c = (char) b;
        System.out.println(Character.hashCode(c));
        System.out.println(Integer.toBinaryString(b << 32));
        System.out.println(Integer.toBinaryString(b << 4));
        System.out.println(Integer.toBinaryString(b << 12));
        System.out.println(Integer.toBinaryString(0x10000 >>> 10));
        System.out.println(Integer.toBinaryString(0xD800));
        System.out.println(Integer.toBinaryString(0b00000000000000000000111110000000));
        System.out.println(0b00000000001110000001111110000000);
        System.out.println(test());

    }
    public static boolean test(){
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 3);
        map.put(3, 1);
        map.put(2, 2);
        BitSet bitset = new BitSet();
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            if(bitset.get(entry.getValue())){
                return false;
            }
        }
        return true;
    }
    public static int humanStringLength(String s){
        if(s == null){
            throw new NullPointerException();
        }
        int count = 0;
        int i = 0;
        while(i < s.length()){
            char c = s.charAt(i);
            if(Character.isHighSurrogate(c)){
                count++;
                i += 2;
            }else if(c > 0x2000 && c < 0x2010){
                if(i + 1 < s.length()){
                    char d = s.charAt(i+1);
                    if(Character.isHighSurrogate(d)){
                        i += 3;
                    }else{
                        i += 2;
                    }
                }
            }else{
                i++;
                count++;
            }
        }
        return count;
    }
}
